﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier.utils;
using Cashier_Service;
using Cashier_Model;
using System.Collections;
using Cashier_Interface;

namespace Cashier.member
{
    public partial class MemberAddForm : Form
    {
        private MainForm mainform;
        private IReloadData iReloadData;
        private long memberid;
        private MemberService memberService;
        private Member member;
        private bool isNew = false; //是否新建

        public MemberAddForm(MainForm mainform, IReloadData iReloadData, String memberno)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.iReloadData = iReloadData;
            this.memberService = new MemberService();

            dateTimePicker1.Value = DateTime.Now.AddYears(-20);
            if (String.IsNullOrEmpty(memberno))
            {
                button_save.Tag = "member_add";
                isNew = true;
            }
            else
            {
                button_save.Tag = "member_edit";
                textbox_memberno.ReadOnly = true;
            }
            Button[] buttons = { button_save };
            RightsforControls.RightsforButton(mainform.Admin.RolePurviewtable, buttons);
            this.InitialFormData(memberno, null);

            if(memberid==0)
                textbox_memberno.LostFocus += new EventHandler(textbox_memberno_LostFocus);
        }

        /// <summary>
        /// 初始化Form数据
        /// </summary>
        protected void InitialFormData(String memberno, Member pMember)
        {
            try
            {
                if (pMember != null)
                    member = pMember;
                else if (!String.IsNullOrEmpty(memberno))
                    member = memberService.GetMember(0, memberno);

                if (member != null)
                {
                    //isNew = false;
                    memberid = member.Id;
                    if (member.Birthday != null)
                        dateTimePicker1.Value = member.Birthday;
                    //FormUtil.InitialFormData(member, this, 1);
                    textbox_memberno.Text = member.Memberno;
                    textbox_name.Text = member.Name;
                    textbox_mobile.Text = member.Mobile;
                    textbox_balance.Text = member.Balance.ToString("f2");
                    if (!String.IsNullOrEmpty(member.Sex))
                    {
                        if (member.Sex.Equals("男"))
                            radioButton_man.Checked = true;
                        else if (member.Sex.Equals("女"))
                            radioButton_woman.Checked = true;
                    }
                    dateTimePicker1.Value = member.Birthday;
                    textbox_address.Text = String.IsNullOrEmpty(member.Address) ? String.Empty : member.Address;
                    textbox_zip.Text = String.IsNullOrEmpty(member.Zip) ? String.Empty : member.Zip;
                    textbox_qq.Text = String.IsNullOrEmpty(member.Qq) ? String.Empty : member.Qq;
                    textbox_email.Text = String.IsNullOrEmpty(member.Email) ? String.Empty : member.Email;
                    textbox_company.Text = String.IsNullOrEmpty(member.Company) ? String.Empty : member.Company;
                    textbox_tel.Text = String.IsNullOrEmpty(member.Tel) ? String.Empty : member.Tel;
                    textbox_fax.Text = String.IsNullOrEmpty(member.Fax) ? String.Empty : member.Fax;
                    textbox_website.Text = String.IsNullOrEmpty(member.Website) ? String.Empty : member.Website;
                    textbox_note.Text = String.IsNullOrEmpty(member.Note) ? String.Empty : member.Note;

                    if (member.Sex.Equals("男"))
                        radioButton_man.Checked = true;
                    else
                        radioButton_woman.Checked = true;
                    textbox_balance.Enabled = false;
                }
                else
                {
                    memberid = 0;
                    isNew = true;
                    textbox_balance.Text = "0.00";
                    textbox_memberno.Text = memberno;
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        #region 功能函数


        #endregion

        /// <summary>
        /// 会员ID失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textbox_memberno_LostFocus(object sender, EventArgs e)
        {
            String memberno = textbox_memberno.Text.Trim();
            this.InitialFormData(memberno, null);
            if (isNew && memberid > 0)
            {
                MessageBox.Show("会员已存在！");
            }
        }

        /// <summary>
        /// 保存事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_save_Click(object sender, EventArgs e)
        {
            try
            {
                int depth = 0;
                ArrayList list = new ArrayList();
                FormUtil.GetValidateControlsNotPassed(this, list, depth);
                if (list.Count > 0)
                    return;

                if (member == null)
                {
                    member = new Member();
                    member.EnterpriseNo = this.mainform.Admin.EnterpriseNo;
                    member.StoreNo = this.mainform.Admin.StoreNo;
                    member.AdminUsername = this.mainform.Admin.Username;
                    member.AdminName = this.mainform.Admin.Name;
                    member.Createtime = DateTime.Now;
                }
                
                //FormUtil.GetFormData(member, this, 1);
                member.Memberno = textbox_memberno.Text.Trim();
                member.Name = textbox_name.Text.Trim();
                member.Mobile = textbox_mobile.Text.Trim();
                member.Balance = textbox_balance.TextValueToDecimal;
                member.Sex = radioButton_man.Checked ? radioButton_man.Text : radioButton_woman.Text;
                member.Birthday = dateTimePicker1.Value;
                member.Address = textbox_address.Text.Trim();
                member.Zip = textbox_zip.Text.Trim();
                member.Qq = textbox_qq.Text.Trim();
                member.Email = textbox_email.Text.Trim();
                member.Company = textbox_company.Text.Trim();
                member.Tel = textbox_tel.Text.Trim();
                member.Fax = textbox_fax.Text.Trim();
                member.Website = textbox_website.Text.Trim();
                member.Note = textbox_note.Text.Trim();

                bool issuccess = false;
                if (member.Id == 0)
                {
                    Member tempMember = memberService.GetMember(0, member.Memberno);
                    if (tempMember != null && tempMember.Id>0)
                    {
                        MessageBox.Show("会员已存在！");
                        this.InitialFormData(String.Empty, tempMember);
                        return;
                    }
                    long id = memberService.InsertMember(member);
                    if (id > 0)
                        issuccess = true;
                }
                else
                {
                    int flag = memberService.UpdateMember(member);
                    if (flag > 0)
                        issuccess = true;
                }
                if (issuccess)
                {
                    this.iReloadData.ReloadDataGridViewData(member.Memberno);
                    DialogResult result = MessageBox.Show("保存成功，是否添加新记录！", "提示", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        textbox_memberno.ReadOnly = false;
                        memberid = 0;
                        isNew = true;
                        member = null;
                        //FormUtil.ClearFormData(this, 1);
                        //textbox_balance.Text = "0.00";

                        textbox_memberno.Text = String.Empty;
                        textbox_name.Text = String.Empty;
                        textbox_mobile.Text = String.Empty;
                        textbox_balance.Text = "0.00";
                        radioButton_man.Checked = true;
                        dateTimePicker1.Value = DateTime.Now.AddYears(-20); ;
                        textbox_address.Text = String.Empty;
                        textbox_zip.Text = String.Empty;
                        textbox_qq.Text = String.Empty;
                        textbox_email.Text = String.Empty;
                        textbox_company.Text = String.Empty;
                        textbox_tel.Text = String.Empty;
                        textbox_fax.Text = String.Empty;
                        textbox_website.Text = String.Empty;
                        textbox_note.Text = String.Empty;

                    }
                    else
                    {
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 退出事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Escape: this.Close();
                    break;
            }
            return b;
        }

        /// <summary>
        /// 单选按钮键盘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 13)
            {
                button_save.Focus();
            }
        }
    }
}
